package td.pt.widget;

import td.client.ui.Button;
import td.domain.Category;
import td.domain.DemoStore;
import td.pt.other.CategoryTreeModel;

import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.cellview.client.CellBrowser;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.RadioButton;
import com.google.gwt.user.client.ui.Widget;
import com.google.gwt.view.client.SingleSelectionModel;
import com.google.inject.Inject;
import com.google.web.bindery.event.shared.EventBus;
import com.gwtplatform.mvp.client.PopupViewImpl;

public class CategoryTreePopupView extends PopupViewImpl implements
		CategoryTreePopupPresenter.MyView {

	private final Widget widget;

	@UiField(provided = true)
	CellBrowser categoryBrowser;
	@UiField
	Button selectCategory;
	@UiField
	Button addToExistingTopic;
	@UiField
	Button addToNewTopic;
	@UiField
	Button cancel;
	@UiField
	Label browserPurposeDescription;

	@UiField
	RadioButton orderAlphabetic;
	@UiField
	RadioButton orderNumberOfThoughts;
	@UiField
	RadioButton orderNumberOfReads;

	CategoryTreeModel model;
	SingleSelectionModel<Category> selectionModel;

	public interface Binder extends UiBinder<Widget, CategoryTreePopupView> {
	}

	@Inject
	public CategoryTreePopupView(final EventBus eventBus, final Binder binder) {
		super(eventBus);

		selectionModel = new SingleSelectionModel<Category>();
		model = new CategoryTreeModel(selectionModel);
		categoryBrowser = new CellBrowser(model, DemoStore.db().rootCategory);

		widget = binder.createAndBindUi(this);
	}

	public void refresh() {
		selectionModel.setSelected(selectionModel.getSelectedObject(), false);
		model.refresh();
	}

	@Override
	public Widget asWidget() {
		return widget;
	}

	public Button getSelectCategory() {
		return selectCategory;
	}

	public Button getAddToExistingTopic() {
		return addToExistingTopic;
	}

	public Button getAddToNewTopic() {
		return addToNewTopic;
	}

	public Button getCancel() {
		return cancel;
	}

	public CellBrowser getCategoryBrowser() {
		return categoryBrowser;
	}

	public SingleSelectionModel<Category> getSelectionModel() {
		return selectionModel;
	}

	public Label getBrowserPurposeDescription() {
		return browserPurposeDescription;
	}

	public RadioButton getOrderAlphabetic() {
		return orderAlphabetic;
	}

	public RadioButton getOrderNumberOfThoughts() {
		return orderNumberOfThoughts;
	}

	public RadioButton getOrderNumberOfReads() {
		return orderNumberOfReads;
	}

}
